package com.linkcard.commen.action;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.linkcard.commen.manager.ImageManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ImageAction extends ActionSupport {
	private ImageManager imageManager=null;
	private File image; // 上传文件的路径
	private String imageContentType;
	private String imageFileName;
	private ByteArrayInputStream inputStream;
	private String directory = null;// 上传的路径
	private JSONObject result = null;
    private String validateCode=null;
	@Override
	public String execute() throws Exception {
		return "success";
	}

	// 上传图片
	@SuppressWarnings("deprecation")
	public String uploadImage() throws Exception {
		result = new JSONObject();
		String imageName = null;

		try {

			String path = ServletActionContext.getRequest().getRealPath(
					directory);
			path = path.replace("/", File.separator).replace("\\",
					File.separator);
			imageName = new Date().getTime()
					+ "."
					+ imageFileName.substring(
							imageFileName.lastIndexOf(".") + 1, imageFileName
									.length());
			File savefile = new File(path, imageName);

			if (!savefile.getParentFile().exists()) {
				savefile.getParentFile().mkdirs();
			}
			FileUtils.copyFile(image, savefile);
		} catch (Exception e) {
			result.put("info", "error");
		}

		result.put("info", (directory + "/" + imageName));
		backText(result.toString());
		return "Data";
	}
	// 上传图片
	@SuppressWarnings("deprecation")
	public String uploadImageForEditor() throws Exception {
		result = new JSONObject();
		String imageName = null;

		try {

			String path = ServletActionContext.getRequest().getRealPath(
					directory);
			path = path.replace("/", File.separator).replace("\\",
					File.separator);
			imageName = new Date().getTime()
					+ "."
					+ imageFileName.substring(
							imageFileName.lastIndexOf(".") + 1, imageFileName
									.length());
			File savefile = new File(path, imageName);

			if (!savefile.getParentFile().exists()) {
				savefile.getParentFile().mkdirs();
			}
			FileUtils.copyFile(image, savefile);
			result.put("error", 0);
		} catch (Exception e) {
			result.put("error", 1);
		}

		result.put("url", ("../../"+directory + "/" + imageName));
		backText(result.toString());
		return "Data";
	}
	/**
	 * 验证码
	 * 
	 * @return
	 */
	public String validateCode() throws Exception{
		inputStream=imageManager.validateCode();
		return "ValidateCode";
	}
	//检查验证码
	@SuppressWarnings("unchecked")
	public String checkValidateCode() throws Exception {
		result=new JSONObject();
		Map session=ActionContext.getContext().getSession();
		if(validateCode.equals(session.get("rand")))
		{
			result.put("info", true);
		}else
		{
			result.put("info", false);
		}
		backText(result.toString());
		return "Data";
	}
	//返回文本
	public void backText(String data) throws Exception
	{
		ServletActionContext.getResponse().setContentType ("text/html;charset=utf-8");
		
		  PrintWriter out = ServletActionContext.getResponse().getWriter();
		  out.print(data);
		  out.flush();
		  out.close();
	}
	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}

	public String getDirectory() {
		return directory;
	}

	public void setDirectory(String directory) {
		this.directory = directory;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public JSONObject getResult() {
		return result;
	}

	public ImageManager getImageManager() {
		return imageManager;
	}

	public void setImageManager(ImageManager imageManager) {
		this.imageManager = imageManager;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}

}
